<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-wms__stock}">
      <wms-form v-model="dataForm" :formList="formList" :table-column="tableColumn" :advanced="advanced"
        @check-change="checkChange" @changeAdvance="changeAdvance" @getDataList="getDataList" @getReset="getReset">
        <el-form-item>
          <el-button type="primary" plain @click="outReelClick">零星出库
          </el-button>
          <el-button type="primary" plain @click="outCheckClick">料箱出库
          </el-button>
          <el-button type="primary" plain @click="exportHandle">库存导出
          </el-button>
          <el-button type="primary" plain @click="lock('1')">库存冻结
          </el-button>
          <el-button type="primary" plain @click="lock('0')">库存解冻
          </el-button>
        </el-form-item>
      </wms-form>
      <wms-table ref="wmsTable" class="stockTable" v-model="dataForm" :page="page" :total="total" :limit="limit"
        :tableData="dataList" :table-column="column" :module-options="mixinViewModuleOptions"
        :dataListLoading="dataListLoading" :showSelection="true" @selectChangeHandle="dataListSelectionChangeHandle"
        @pageSizeChangeHandle="pageSizeChangeHandle" @pageCurrentChangeHandle="pageCurrentChangeHandle"
        @changeLockSt="changeLockSt" />
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./components/stock-add-or-update";
import debounce from "lodash/debounce";
import { formList, tableColumn } from './config'
import wmsTable from '@/components/wms-table'
import wmsForm from '@/components/wms-form'
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/wms/stock/page",
        getDataListIsPage: true,
        exportURL: "/wms/stock/export",
        deleteURL: "/wms/stock",
        deleteIsBatch: true,
        deleteIsBatchKey: 'stockId',
        
      },
      dataForm: {
        spuNo: '',
        spuName: '',
        contNo: '',
        curPosition: '',
        stockType:''
      },
      changeForm: {
        stockId: '',
        lockSt: ''
      },
      stockQty:null,
      formList,
      tableColumn,
      column: [],
      advanced: false,
      outForm:{
        stockIds: [],
        requestType: '',
        stn:'',
      },
      lockForm:{
        stockIds: [],
        lockType: '',
      },
      outUrl:'/wms/task/request-out',
      lockUrl:'/wms/stock/lock'
    };
  },
  components: {
    AddOrUpdate,
    wmsTable,
    wmsForm
  },
  methods: {
    lock(lockType){

      if (this.lockForm.stockIds.length==0) {
        this.$message({
          
              message:'请选择库存',
              type:'error',
              center:true,
              showClose:true
          })
        //this.$refs.callAgvItemID.focus();
      } 
      else
      {
        this.lockForm.lockType = lockType
        this.$http['post'](this.lockUrl, this.lockForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message({
              message: res.msg,
              type: 'error',
              duration: 5000,})
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.visible = false
              this.query()
              this.outForm.stockIds = []
              this.lockForm.stockIds = []
            }
          })
        }).catch(() => { })
      }
      


    },

    dataListSelectionChangeHandle(selection, row) {

      // this.dataForm.stockId = ''; //每次单选都重置id
      // if (selection.length > 1) {
      //   this.$refs['wmsTable'].$refs.Table.clearSelection()
      //   this.$refs['wmsTable'].$refs.Table.toggleRowSelection(row)
      // }
      // if (selection.length != 0) {
      //   //只有当真正选中了目标主单才获取id
      //   this.dataForm.stockId = row.stockId;
      // }

      this.outForm.stockIds = null
      if (selection.length > 0) {
        //支持多选
        this.outForm.stockIds = selection.map(item => item.stockId)
        this.lockForm.stockIds = selection.map(item => item.stockId)
      }
      else
      {
        this.outForm.stockIds = []
        this.lockForm.stockIds = []
      }
    },
    //1.库存冻结
    changeLockSt: debounce(function (val, row) {
      var tempStatus = row.lockSt === 0 ? 1 : 0;
      this.changeForm.stockId = row.stockId;
      this.changeForm.lockSt = row.lockSt;
      this.$http[!this.changeForm.stockId ? 'post' : 'put']('/wms/stock/', this.changeForm).then(({ data: res }) => {
        if (res.code !== 0) {
          //如果更新失败则重置为原来的状态
          row.lockSt = tempStatus;
          return this.$message.error(res.msg)
        }
        if (this.changeForm.lockSt === 1) {
          this.$message({
            message: this.$t('prompt.freezed'),
            type: 'success',
            duration: 1000,
            onClose: () => {
            }
          })
        }
        if (this.changeForm.lockSt === 0) {
          this.$message({
            message: this.$t('prompt.thaw'),
            type: 'success',
            duration: 1000,
            onClose: () => {
            }
          })
        }
      }).catch(() => { })
    }),
    freezeHandle(id) {
      if (!this.dataForm.stockId) {
        return this.$message({
          message: this.$t('prompt.freeze'),
          type: 'warning',
          duration: 1000
        })
      }
      this.$confirm(this.$t('prompt.info', { 'handle': this.$t('freeze') }), this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$http.post('/wms/stock/freeze/' + this.dataForm.stockId)
          .then(({ data: res }) => {
            if (res.code != 0) {
              return this.$message.error(res.msg)
            }
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.getDataList()
              }
            })
          }).catch(() => { })


      }).catch(() => { })

    },
    //零星出库
    outReelClick(){
      
      this.outForm.requestType='1'
      this.requestOut()
    
    },
    //盘点出库
    outCheckClick(){
      this.outForm.requestType='2'
      this.requestOut()

    },

    //导出
    // exportClick(){
      
    //   this.$refs['wmsTable'].$refs.Table.exportClick()

    // },
    //请求料箱出库
    requestOut(){
      
      this.outForm.stn = this.$store.state.user.stn
      if (this.outForm.stockIds.length==0) {
        this.$message({
          
              message:'请选择库存',
              type:'error',
              center:true,
              showClose:true
          })
        //this.$refs.callAgvItemID.focus();
      } 
      else if(this.outForm.requestType==''){
        this.$message({
          
          message:'无出库类型',
          type:'error',
          center:true,
          showClose:true
         })
      }
      else {
        
        this.$http['post'](this.outUrl, this.outForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.visible = false
              //this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      }

    },
  }
}
</script>
<style scoped lang="scss">
/* 隐藏表头的全选框 */
/* ::v-deep .stockTable thead .el-table-column--selection .cell {
  display: none;
} */
</style>
